unit FileOptions;

interface

uses IniFiles, Windows, SysUtils, CRMClasses;

type
  TFileOption = class
  private
    FIniFile: TIniFile;
    function GetItem(AName: string): string;
    procedure SetItem(AName: string; const Value: string);
    function GetInteger(AName: string): integer;
    procedure SetInteger(AName: string; const Value: integer);
  public
    constructor Create;
    destructor Destroy; override;
    property Items[AName: string]: string read GetItem write SetItem; default;
    property AsInteger[AName: string]: integer read GetInteger write SetInteger;
  end;

implementation

const
  SECTION = 'OPTIONS';

{ TFileOption }

constructor TFileOption.Create;
var
  file_path: string;
begin
  ObjCounter.IncCount(self.ClassName);
  file_path := ExtractFilePath(ParamStr(0)) + StringReplace(ExtractFileName(ParamStr(0)), ExtractFileExt(ParamStr(0)), '', []);
  FIniFile := TIniFile.Create(file_path + '.ini');
end;

destructor TFileOption.Destroy;
begin
  FIniFile.Free;
  ObjCounter.DecCount(self.ClassName);  
end;

function TFileOption.GetInteger(AName: string): integer;
var
  s: string;
  test_int: integer;
begin
  s := FIniFile.ReadString(SECTION, AName, '');
  if TryStrToInt(s, test_int) then
    Result := test_int
  else
    Result := -1;
end;

function TFileOption.GetItem(AName: string): string;
begin
  Result := FIniFile.ReadString(SECTION, AName, '');
end;

procedure TFileOption.SetInteger(AName: string; const Value: integer);
begin
  FIniFile.WriteInteger(SECTION, AName, Value);
  FIniFile.UpdateFile;
end;

procedure TFileOption.SetItem(AName: string; const Value: string);
begin
  FIniFile.WriteString(SECTION, AName, Value);
  FIniFile.UpdateFile;  
end;

end.
